package cn.e3mall.sso.controller;

import cn.e3mall.common.utils.CookieUtils;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.pojo.TbUser;
import cn.e3mall.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**   
 * @description  
 * @author lenovo   
 * @date 2018年5月17日 下午8:26:50 
 * @version 1.0.0  
 * @
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${TOKEN_KEY}")
    private String TOKEN_KEY;

    /** 
     * @description 验证用户的注册数据
     * @param param
     * @param type
     * @return
     * @exception
     * @author lenovo
     * @date 2018年5月17日 下午8:27:38
     * @version 1.0.0
     */
    @RequestMapping("/user/check/{param}/{type}")
    public E3Result checkUserData(@PathVariable String param, @PathVariable Integer type) {
        E3Result result = userService.checkUserData(param, type);
        return result;
    }

    /** 
     * @description 用户注册
     * @param tbUser
     * @return
     * @exception
     * @author lenovo
     * @date 2018年5月17日 下午8:28:39
     * @version 1.0.0
     */
    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    @ResponseBody
    public E3Result register(TbUser tbUser) {
        E3Result e3Result = userService.register(tbUser);
        return e3Result;
    }

    /** 
     * @description 用户登录
     * @param username
     * @param password
     * @param request
     * @param response
     * @return
     * @exception
     * @author lenovo
     * @date 2018年5月17日 下午8:28:49
     * @version 1.0.0
     */
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    @ResponseBody
    public E3Result login(String username, String password, HttpServletRequest request, HttpServletResponse response){
        E3Result e3Result = userService.login(username, password);
        //把token写入cookie
        CookieUtils.setCookie(request, response, TOKEN_KEY, e3Result.getData().toString());
        return  e3Result;
    }

    /** 
     * @description 使用token值来获取用户的数据
     * @param token
     * @return
     * @exception
     * @author lenovo
     * @date 2018年5月17日 下午8:29:04
     * @version 1.0.0
     */
    @RequestMapping(value = "/user/token/{token}",method = RequestMethod.GET)
    @ResponseBody
    public E3Result getUserByToken(@PathVariable String token){
        E3Result e3Result = userService.getUserByToken(token);
        return  e3Result;
    }

    /** 
     * @description 退出登录
     * @param token
     * @return
     * @exception
     * @author lenovo
     * @date 2018年5月17日 下午8:29:27
     * @version 1.0.0
     */
    @RequestMapping(value = "/user/logout/{token}",method = RequestMethod.GET)
    @ResponseBody
    public E3Result logout(@PathVariable String token){
        E3Result e3Result = userService.logout(token);
        return  e3Result;
    }
}
